//
//  ClubTextImageCell.m
//  Shengzhuli
//
//  Created by FXGuo on 2018/1/18.
//  Copyright © 2018年 kamangkeji. All rights reserved.
//

#import "ClubTextImageCell.h"
#import <UIImageView+WebCache.h>
#import "YYText.h"
#import "YYImage.h"
#import "NSAttributedString+YYText.h"

#define TextFont 16
#define LineSpace 8
#define KernSpace 3

@interface ClubTextImageCell()

@property (nonatomic,weak) YYLabel *yyLab;
@property (nonatomic,weak) UIImageView *imgView;
@property (nonatomic,weak) UIView *bgimgView;

@end

@implementation ClubTextImageCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        YYLabel *yyLab = [[YYLabel alloc]init];
        yyLab.numberOfLines = 0;
        yyLab.preferredMaxLayoutWidth = ScreenW-20;
        self.yyLab = yyLab;
        [self.contentView addSubview:yyLab];

        UIView *bgimgView = [[UIView alloc]init];
        self.bgimgView = bgimgView;
        bgimgView.layer.shadowColor = [UIColor blackColor].CGColor;
        bgimgView.layer.shadowOpacity = 0.7f;
        bgimgView.layer.shadowRadius = 4.f;
        bgimgView.layer.shadowOffset = CGSizeMake(4,4);
        [self.contentView addSubview:bgimgView];
        
        UIImageView *imgView = [[UIImageView alloc]init];
        imgView.layer.cornerRadius = 5.0;
        imgView.clipsToBounds = YES;
        self.imgView = imgView;
        [bgimgView addSubview:imgView];
    }
    return self;
}
- (void)setImgStr:(NSString *)imgStr
{
    _imgStr = imgStr;
    self.imgView.image = GetImage(imgStr);
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:@"北国风光\n 千里冰封，万里雪飘\n如果冬天只允许我去一座城市\n那么我一定选择门头沟瓜草地基地"];
    attri.yy_font = [UIFont systemFontOfSize:TextFont];
    attri.yy_color = [UIColor blackColor];
    attri.yy_alignment = NSTextAlignmentCenter;
    attri.yy_lineSpacing = LineSpace;
    attri.yy_kern = @(KernSpace);
    
    self.yyLab.attributedText = attri;
}
- (void)setShadowOffset:(float)shadowOffset
{
    _shadowOffset = shadowOffset;
//    self.bgimgView.layer.shadowOffset = CGSizeMake(shadowOffset,shadowOffset);
}
#pragma mark -----------------------俱乐部详情--------------------------
- (void)setModel:(ImageTitleModel *)model
{
    _model = model;
    [self.imgView sd_setImageWithURL:[NSURL URLWithString:model.clubImage] placeholderImage:GetImage(@"default_large_goods_image")];
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:model.clubContent];
    attri.yy_font = [UIFont systemFontOfSize:TextFont];
    attri.yy_color = [UIColor blackColor];
    attri.yy_alignment = NSTextAlignmentCenter;
    attri.yy_lineSpacing = LineSpace;
    attri.yy_kern = @(KernSpace);
    
    //计算文本尺寸
    CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 40, MAXFLOAT);
    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:maxSize text:attri];
    self.yyLab.textLayout = layout;
    self.yyLab.attributedText = attri;
}
- (CGFloat)calculateDetailCellHWithModel:(ImageTitleModel *)model
{
    CGFloat maxY = 20;
    maxY += [self getTextH:model.clubContent]+10;
    CGFloat imageH = [self getImageH:model.clubImage];
    maxY += imageH == 0?0:(imageH+10);
    return maxY;
}
#pragma mark -----------------------推荐详情--------------------------
- (void)setCommendModel:(CommendImageTextModel *)commendModel
{
    _commendModel = commendModel;
    [self.imgView sd_setImageWithURL:[NSURL URLWithString:commendModel.image] placeholderImage:GetImage(@"default_large_goods_image")];
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:commendModel.content];
    attri.yy_font = [UIFont systemFontOfSize:TextFont];
    attri.yy_color = [UIColor blackColor];
    attri.yy_alignment = NSTextAlignmentLeft;
    attri.yy_lineSpacing = LineSpace;
    attri.yy_kern = @(KernSpace);
    
    //计算文本尺寸
    CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 40, MAXFLOAT);
    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:maxSize text:attri];
    self.yyLab.textLayout = layout;
    self.yyLab.attributedText = attri;
}
- (CGFloat)calculateComendCellHWithModel:(CommendImageTextModel *)model
{
    CGFloat maxY = 20;
    maxY += [self getTextH:model.content]+10;
    CGFloat imageH = [self getImageH:model.image];
    maxY += imageH == 0?0:imageH+10;
    return maxY;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    if (self.model) {
        self.yyLab.frame = CGRectMake(20, 20, ScreenW-40, [self getTextH:self.model.clubContent]);
        CGFloat imageH = [self getImageH:self.model.clubImage];
        self.bgimgView.frame = CGRectMake(20, CGRectGetMaxY(self.yyLab.frame)+10, ScreenW-40, imageH);
        self.imgView.frame = self.bgimgView.bounds;
    }else if (self.commendModel) {
        self.yyLab.frame = CGRectMake(20, 20, ScreenW-40, [self getTextH:self.commendModel.content]);
        CGFloat imageH = [self getImageH:self.commendModel.image];
        self.bgimgView.frame = CGRectMake(20, CGRectGetMaxY(self.yyLab.frame)+10, ScreenW-40, imageH);
        self.imgView.frame = self.bgimgView.bounds;
    }
}

- (CGFloat)getTextH:(NSString *)str
{
    if (!str) return 0;
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:str];
    attri.yy_font = [UIFont systemFontOfSize:TextFont];
    attri.yy_color = [UIColor blackColor];
    attri.yy_lineSpacing = LineSpace;
    attri.yy_kern = @(KernSpace);
    
    CGSize maxSize = CGSizeMake(ScreenW-40, MAXFLOAT);
    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:maxSize text:attri];
    return layout.textBoundingSize.height;
}
- (CGFloat)getImageH:(NSString *)imageStr
{
    if (imageStr == nil || imageStr.length == 0) return 0;
    UIImage *image = [[[SDWebImageManager sharedManager] imageCache] imageFromDiskCacheForKey:imageStr];
    if (image) {
        CGFloat imageH = (ScreenW-40)*image.size.height/image.size.width*1.0;
        return imageH;
    }
    return 200;
}


@end
